home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / qrt.lzh / LEXER.C < prev    next >
C/C++ Source or Header  |  1989-02-16  |  5KB  |  256 lines

  1.  
  2. /**********************************************************
  3.  
  4.       Small lexical analyser for inout. Also contains
  5.       some bounds checking code.
  6.  
  7.  **********************************************************/
  8.  
  9.  
  10. #include "qrt.h"
  11.  
  12. extern int linenumber;
  13. char *malloc();
  14.  
  15. # ifdef AMIGA
  16.     float atof();
  17. # endif
  18.  
  19. /**********************************************************
  20.  
  21.            Transforms all white spaces to blanks
  22.  
  23.  **********************************************************/
  24.  
  25. char towhite(c) char c; {
  26.   if (c=='\n') linenumber++;
  27.  
  28.   if ((c=='\n') ||
  29.       (c=='=')  ||
  30.       (c==',')  ||
  31.       (c==';'))     return(' ');
  32.  
  33.   return(c);
  34. }
  35.  
  36.  
  37. /**********************************************************
  38.  
  39.         Removes blank space before next token
  40.  
  41.  **********************************************************/
  42.  
  43. rmspace() {
  44.   char c;
  45.   while (((c=towhite(fgetc(stdin)))==' ') && !feof(stdin));
  46.   ungetc(c,stdin);
  47. }
  48.  
  49. /**********************************************************
  50.  
  51.  Comment Killer - Added 16 Jun 88 to handle nested comments
  52.  
  53.  **********************************************************/
  54.  
  55. Comment_Killer()
  56. {
  57.   char c;
  58.  
  59.   c='\0';
  60.   while (c != '}' && !feof(stdin)) {
  61.     c = towhite(fgetc(stdin));
  62.     if (c == '{') Comment_Killer();
  63.   }
  64. }
  65.  
  66. /**********************************************************
  67.  
  68.                Get next token from stdin
  69.  
  70.  **********************************************************/
  71.  
  72. GetToken(s)                          /* get a token from stdio */
  73.   char s[];
  74. {                                    /* */
  75.   char c; int x;
  76.  
  77.   x=0; s[0]=c='\0';                       /* char count */
  78.  
  79.   rmspace();
  80.  
  81.   while (!feof(stdin) && x<(SLEN-1)) {
  82.     s[x++]=c=toupper(towhite(fgetc(stdin)));
  83.     if (c==' ') { s[--x]='\0'; break; }
  84.     if (c=='(' || c==')') {
  85.       if (x==1) { s[x]='\0'; break; }
  86.       else {
  87.         ungetc(c,stdin); s[--x]='\0'; break;
  88.       }
  89.     }
  90.     if (c=='{') {
  91.       x--;
  92.       Comment_Killer();
  93.       rmspace();
  94.     }
  95.   }
  96. }
  97.  
  98.  
  99. /**********************************************************
  100.  
  101.       Return value if color is in range 0<=cnum<=CNUM
  102.       otherwise call error routine.
  103.  
  104.  **********************************************************/
  105.  
  106. float InRange(cnum)
  107.   float cnum;
  108. {
  109.   if (cnum>=0 && cnum<=1.00) return(cnum);
  110.   Error(COLOR_VALUE_ERR,1501);
  111. }
  112.  
  113.  
  114. /**********************************************************
  115.  
  116.          Return value if value is >=0.
  117.          otherwise call error routine.
  118.  
  119.  **********************************************************/
  120.  
  121. float IsPos(val)
  122.   float val;
  123. {
  124.   if (val >= 0) return(val);
  125.   Error(LESS_THAN_ZERO,1502);
  126. }
  127.  
  128. /**********************************************************
  129.  
  130.     Reads next number and converts to float from string
  131.  
  132.  **********************************************************/
  133.  
  134. float Get_Next_Num() {
  135.   char str[SLEN];
  136.   float val;
  137.  
  138.   GetToken(str);
  139.  
  140.   val=atof(str);
  141.  
  142. # ifdef IODEBUG
  143.     printf("GETNEXTNUM: token=%s, val=%f\n",str,val);
  144. # endif
  145.  
  146.   return(val);
  147. }
  148.  
  149.  
  150. /**********************************************************
  151.  
  152.       Reads a name from input, and allocates some space
  153.       for it. Returns a pointer to space.
  154.  
  155.  **********************************************************/
  156.  
  157. char *Get_Next_Name() {
  158.   char str[SLEN], *s;
  159.  
  160.   GetToken(str);
  161.  
  162.   if ((s=malloc(strlen(str)))==NULL)
  163.     Error(MALLOC_FAILURE,1503);
  164.  
  165.   strcpy(s,str);
  166.  
  167. # ifdef IODEBUG
  168.     printf("GETNEXTNAME: token=%s\n",str);
  169. # endif
  170.  
  171.   return(s);
  172. }
  173.  
  174. /**********************************************************
  175.  
  176.      Reads a number 0..1 and returns a color value
  177.      0..CNUM;
  178.  
  179.  **********************************************************/
  180.  
  181. short Get_Color_Val() {
  182.   return((short)(InRange(Get_Next_Num())*(float)CNUM));
  183. }
  184.  
  185.  
  186. /**********************************************************
  187.  
  188.        Returns true if the next token is a left paren
  189.  
  190.  **********************************************************/
  191.  
  192. GetLeftParen() {
  193.   char str[SLEN];
  194.  
  195.   GetToken(str);
  196.   if (strcmp(str,"(")!=0) Error(LPAREN_EXPECTED,1504);
  197.   return;
  198. }
  199.  
  200.  
  201. /**********************************************************
  202.  
  203.        Returns true if the next token is a left paren
  204.  
  205.  **********************************************************/
  206.  
  207. int GetRightParen() {
  208.   char str[SLEN];
  209.  
  210.   GetToken(str);
  211.   if (strcmp(str,")")!=0) return(FALSE);
  212.   return(TRUE);
  213. }
  214.  
  215.  
  216. /**********************************************************
  217.  
  218.            Gets a VECTOR structure of the form
  219.            (num1, num2, num3)
  220.  
  221.  **********************************************************/
  222.  
  223. GetVector(vector)
  224.   VECT_PTR vector;
  225. {
  226.   GetLeftParen();
  227.  
  228.   vector->x = Get_Next_Num();
  229.   vector->y = Get_Next_Num();
  230.   vector->z = Get_Next_Num();
  231.  
  232.   if(!GetRightParen()) Error(ILLEGAL_VECTOR,1505);
  233. }
  234.  
  235.  
  236. /**********************************************************
  237.  
  238.            Gets a SVECTOR structure of the form
  239.            (num1, num2, num3)
  240.  
  241.  **********************************************************/
  242.  
  243. GetSVector(svector)
  244.   SVECT_PTR svector;
  245. {
  246.   GetLeftParen();
  247.  
  248.   svector->r = Get_Color_Val();
  249.   svector->g = Get_Color_Val();
  250.   svector->b = Get_Color_Val();
  251.  
  252.   if(!GetRightParen()) Error(ILLEGAL_SVECTOR,1506);
  253. }
  254.  
  255.  
  256.